////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __WN_FILESYSTEM_H__
#define __WN_FILESYSTEM_H__

#include "WNMemory/inc/WNMemory.h"
#include "WNStrings/inc/WNStrings.h"

__WN_INLINE WN_SIZE_T WNGetPathFromFilename(const WN_CHAR* _file) {
    const WN_CHAR* tempFile = _file;
    WN_SIZE_T braceChar = 0;

    for (; *tempFile != '\0'; ++tempFile) {
        if (*tempFile == '/' || *tempFile == '\\') {
            braceChar = tempFile - _file;
        }
    }

    return(braceChar > 0? braceChar + 1:  braceChar);
}

__WN_INLINE WN_BOOL WNAppendPath(WN_CHAR* _path, const WN_CHAR* _append, WN_SIZE_T _maxSize) {
    WN_SIZE_T pathLen = WNStrLen(_path);
    WN_SIZE_T appendLen = WNStrLen(_append);
    WN_BOOL addPath = WN_FALSE;

    if (_path[pathLen-1] != '/' && _path[pathLen-1] != '\\' ) {
        pathLen += 1;
        addPath = WN_TRUE;
    }

    if (_maxSize < appendLen + pathLen) {
        return(WN_FALSE);
    }

    if (addPath) {
       _path[pathLen - 1] = '/';
    }

    WNMemCpy(_path + pathLen, _append, appendLen);

    _path[pathLen + appendLen] = '\0';

    return(WN_TRUE);
}

#endif // __WN_FILESYSTEM_H__